package com.czhlearn.controller;

import com.czhlearn.pojo.Result;
import com.czhlearn.utils.AliOssUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传
 * @program: big-event
 * @description:
 * @author: czh
 * @create: 2023-12-20 11:29
 **/

@RestController
public class FileUploadController {


    /**
     * 文件上传
     * @param file 文件路径
     * @return
     * @throws IOException
     *
     * LTAI5tPLh9H3n4nheVP4t7Wr
     *
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {

        //获取文件内容的输入流，写入到本地磁盘文件
        String originalFilename = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("E:\\VSStytem\\SpringLearn\\big-event\\src\\main\\resources\\files\\"+filename));
        String url = AliOssUtils.uploadFile(filename, file.getInputStream());
        return Result.success(url);

    }
}
